{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Camera manipulation\n",
    "\n",
    "An example of camera motion following an object.\n",
    "Blue line is a camera path and the object is moving on the red line. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "from numpy import sin,cos,pi\n",
    "from ipywidgets import interact, IntSlider"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = k3d.plot()\n",
    "\n",
    "plt_arrow = k3d.vectors([0,0,0],[1,1,1])\n",
    "plot += plt_arrow\n",
    "\n",
    "N = 100\n",
    "cam_traj = [np.array([-sin(phi), -cos(phi), phi*0.15],dtype=np.float32) for phi in np.linspace(0,2*pi,N)]\n",
    "object_traj = [np.array([sin(phi),-1+cos(phi)+sin(2*phi),0.03*phi],dtype=np.float32) for phi in np.linspace(0,2*pi,N)]\n",
    "\n",
    "plt_cam_traj = k3d.line(cam_traj ,shader='simple')\n",
    "plt_object_traj = k3d.line(object_traj, color=0xff0000,shader='mesh')\n",
    "\n",
    "plt_cam_pos = k3d.points([[0, -1, 0]],point_size=0.1,shader='mesh')\n",
    "plt_object_pos = k3d.points([[0, -1, 0]],color=0xff0000,point_size=0.1,shader='mesh')\n",
    "\n",
    "plot += plt_cam_traj\n",
    "plot += plt_object_traj\n",
    "plot += plt_object_pos\n",
    "plot += plt_cam_pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_auto_fit = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@interact(ith=IntSlider(value=80,min=0,max=N-1))\n",
    "def g(ith):\n",
    "    cam_pos = cam_traj[ith]\n",
    "    object_pos = object_traj[ith]\n",
    "    plot.camera = cam_pos.tolist()+\\\n",
    "                  object_pos.tolist()+\\\n",
    "                  [0,0,1] \n",
    "    \n",
    "    cam_dir = np.array(object_pos,dtype=np.float32) - cam_pos\n",
    "   \n",
    "    plt_cam_pos.positions = [cam_pos]\n",
    "    plt_object_pos.positions = [object_pos]\n",
    "\n",
    "    plt_arrow.origins = cam_pos\n",
    "    plt_arrow.vectors = cam_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_reset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
